from django.shortcuts import render
from rest_framework.generics import ListAPIView
# Create your views here.
from rest_framework.filters import OrderingFilter
from .serializer import SKUSerializers, SKUIndexSerializers
from .models import SKU
from drf_haystack.viewsets import HaystackViewSet


class GoodsListView(ListAPIView):
    """
    商品列表展示
    get /goods/(?P<cat1>\d+)/skus/?page=1&page_size=4&ordering=price
    """
    # 序列化器 SKUSerializers
    serializer_class = SKUSerializers

    # 排序
    filter_backends = [OrderingFilter]
    # 要过滤的字段
    ordering_fields = ('create_time', 'price', 'sales')
    # 分页, 在全局中配置

    def get_queryset(self):
        """由于所有商品包含了未上架的商品,不满足需求"""
        category_id = self.kwargs['category_id']
        return SKU.objects.filter(category_id=category_id, is_launched=True)


class SKUSearchViewSet(HaystackViewSet):
    """
    SKU搜索
    """
    index_models = [SKU]

    serializer_class = SKUIndexSerializers










